﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace viewevents
{
    public partial class Form1 : Form
    {
        private static string[] eventTypes =
        {
            "gameevent",
            "playerevent",
            "playerunitevent",
            "unitevent",
            "dialogevent",
            "widgetevent"
        };

        private const string COMMON_J = "common.j";
        private const string EVENT_TEXT = "event.txt";
        private const string DEFINE_TXT = "define.txt";
        private Dictionary<string, List<string>> _eventType2Events = new Dictionary<string, List<string>>();
        private Dictionary<string, string> _event2Desc = new Dictionary<string, string>();
        private Dictionary<string, string> _eventName2Chinese= new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();
            InitCustom();
        }

        private void InitCustom()
        {
            for (int i = 0; i < eventTypes.Length; i++)
            {
                ListViewItem item = new ListViewItem(eventTypes[i], i);
                int pos = eventTypes[i].IndexOf("event");
                item.Name = eventTypes[i].Substring(0, pos);
                eventTypeList.Items.Add(item);
            }
            
            if (!File.Exists(COMMON_J))
            {
                MessageBox.Show("为找到common.j");
                return;
            }

            if (!File.Exists(DEFINE_TXT))
            {
                MessageBox.Show("未找到define.txt");
                return;
            }

            using (FileStream fs = new FileStream(COMMON_J, FileMode.Open, FileAccess.Read))
            {
                Regex re = new Regex(@"event\s+EVENT", RegexOptions.Compiled);
                Regex re1 = new Regex(@"\s+(\w+)event\s+(\w+)\s+");
                StreamReader sr = new StreamReader(fs);
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    if (re.IsMatch(s))
                    {
                        MatchCollection mc = re1.Matches(s);
                        for (int i = 0; i < mc.Count; i++)
                        {
                            GroupCollection groups = mc[i].Groups;
                            if (groups.Count < 3) continue;
                            if (!_eventType2Events.TryGetValue(groups[1].Value, out var eventList))
                            {
                                _eventType2Events[groups[1].Value] = new List<string>();
                                eventList = _eventType2Events[groups[1].Value];
                            }
                            eventList.Add(groups[2].Value);
                        }
                    }
                }
            }

            using (FileStream fs = new FileStream(EVENT_TEXT, FileMode.Open, FileAccess.Read))
            {
                Regex re = new Regex(@"^\[{1}(\w+)\]{1}$");
                StreamReader sr = new StreamReader(fs);
                string s;
                while ((s = sr.ReadLine()) != null)
                {
                    if (re.IsMatch(s))
                    {
                        GroupCollection mgc = re.Match(s).Groups;
                        if (mgc.Count < 2)
                        {
                            continue;
                        }

                        string content = "";
                        while ((s = sr.ReadLine()) != null)
                        {
                            if (string.IsNullOrEmpty(s.Trim()))
                            {
                                break;
                            }
                            content = content + s + '\n';
                        }

                        string key = mgc[1].Value;
                        if (mgc[1].Value.StartsWith("TriggerRegister"))
                        {
                            key = mgc[1].Value.Substring("TriggerRegister".Length,
                                mgc[1].Value.Length - "TriggerRegister".Length).ToLower();
                        }
                        string endkey = "";
                        if (key.EndsWith("eventbj"))
                        {
                            endkey = "eventbj";
                        }else if (key.EndsWith("event"))
                        {
                            endkey = "event";
                        }else if (key.EndsWith("bj"))
                        {
                            endkey = "bj";
                        }

                        if (!string.IsNullOrEmpty(endkey))
                        {
                            key = key.Substring(0, key.Length - endkey.Length);
                        }

                        _event2Desc[key.ToLower()] = content;
                    }
                }
            }

            ParseDefine();
        }

        private void ParseDefine()
        {
            Regex re = new Regex(@"EVENT_(\w+),.?\((\d+)\)(\w+)\s-\s(\w+).?", RegexOptions.Compiled);
            using (FileStream fs = new FileStream(DEFINE_TXT, FileMode.Open, FileAccess.Read))
            {
                StreamReader sr = new StreamReader(fs);
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    if (re.IsMatch(line))
                    {
                        Match mc = re.Match(line);
                        if (mc.Groups.Count < 4)
                        {
                            continue;;
                        }

                        string k = "EVENT_" + mc.Groups[1].Value;
                        if (!_eventName2Chinese.TryGetValue(k, out var chinsesDesc))
                        {
                            _eventName2Chinese[k] = mc.Groups[3].Value + " - " + mc.Groups[4].Value;
                        }
                    }
                }
            }
        }

        private void DumpEventLists()
        {
            if (File.Exists("event_desc.txt"))
            {
                File.Delete("event_desc.txt");
            }

            using (FileStream fs = File.Create("event_desc.txt"))
            {
                foreach (KeyValuePair<string, List<string>> eventPair in _eventType2Events)
                {
                    foreach (var eventName in eventPair.Value)
                    {
                        string s = new StringBuilder().AppendFormat("{0} = {1}\n", eventName.ToString().ToLower(), "").ToString();
                        byte[] info = new UTF8Encoding(true).GetBytes(s);
                        fs.Write(info, 0, s.Length);
                    }

                }
            }

        }

        private void eventTypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = eventTypeList.SelectedItems;
            for (int i = 0; i < items.Count; i++)
            {
                List<string> events = _eventType2Events[items[i].Name];
                Console.WriteLine(items[i].Name + events.Count);
                eventsList.Items.Clear();
                foreach (var name in events)
                {
                    ListViewItem item = new ListViewItem(name);
                    item.Name = name;
                    eventsList.Items.Add(item);
                }
            }
        }

        private void eventsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = eventsList.SelectedItems;
            for (int i = 0; i < items.Count; i++)
            {
                string key = items[i].Name;
                if (_eventName2Chinese.TryGetValue(key, out var desc))
                {
                    contentRichLabel.Text = desc;
                }
                else
                {
                    contentRichLabel.Text = "";
                }
                searchBox.Text = key;
            }
        }
    }
}
